<?php
/**
 * Created by PhpStorm.
 * User: 1
 * Date: 2017-08-01
 * Time: 15:29
 */

namespace app\admin\controller;


use app\src\admin\helper\ByApiHelper;
use app\src\sunsun\adt\logic\AdtDeviceLogic;
use app\src\sunsun\adt\model\AdtDevice;
use app\src\sunsun\aph300\logic\Aph300DeviceLogic;
use app\src\sunsun\aph300\model\Aph300Device;
use app\src\sunsun\aq806\logic\Aq806DeviceLogic;
use app\src\sunsun\aq806\model\Aq806Device;
use app\src\sunsun\cp1000\logic\Cp1000DeviceLogic;
use app\src\sunsun\cp1000\model\Cp1000Device;
use app\src\sunsun\filterVat\logic\FilterVatDeviceLogic;
use app\src\sunsun\filterVat\model\FilterVatDevice;
use app\src\sunsun\heatingRod\logic\HeatingRodDeviceLogic;
use app\src\sunsun\heatingRod\model\HeatingRodDevice;
use app\src\sunsun\water_pump\logic\WaterPumpDeviceLogic;
use app\src\sunsun\water_pump\model\WaterPumpDevice;
use think\Controller;

class SunsunDevice extends Controller
{
    public function boye_display($theme='default',$file=false){
        if(!empty($file)){
            return $this->fetch($theme.'/'.$file);
        }else{
            return $this->fetch($theme."/". request()->controller().'/'.request()->action());
//        return $this->fetch("default/". request()->controller().'/'.request()->action());
        }
    }

    public function force_offline(){
        $map['tcp_client_id'] = ['neq',''];
        (new Aq806DeviceLogic())->save($map,['tcp_client_id'=>'']);
        (new Aph300DeviceLogic())->save($map,['tcp_client_id'=>'']);
        (new WaterPumpDeviceLogic())->save($map,['tcp_client_id'=>'']);
        (new HeatingRodDeviceLogic())->save($map,['tcp_client_id'=>'']);
        (new FilterVatDeviceLogic())->save($map,['tcp_client_id'=>'']);
        (new AdtDeviceLogic())->save($map,['tcp_client_id'=>'']);
        (new Cp1000DeviceLogic())->save($map,['tcp_client_id'=>'']);
        $this->success('操作成功');
    }

    public function online(){
        $list = [];
        array_push($list,$this->getAq806Info());
        array_push($list,$this->getAph300Info());
        array_push($list,$this->getWaterPumpInfo());
        array_push($list,$this->getHeatingRodInfo());
        array_push($list,$this->getFilterInfo());
        array_push($list,$this->getAdtInfo());
        array_push($list,$this->getCp1000Info());
        usort($list,function($a,$b){
            if($a['count'] === $b['count']){
                return 0;
            }else{
                return ($a['count'] > $b['count']) ? -1 : 1;
            }
        });
        $total = 0;
        $true_total = 0;
        foreach($list as $key => $value) {
            $total += intval($value['count']);
            $true_total += intval($value['true_count']);
        }

        if (defined("UID")) {
            $this->assign("show_btn", 1);
        } else {
            $this->assign("show_btn", 0);
        }
        $this->assign('list',$list);
        $this->assign('true_total',$true_total);
        $this->assign('total',$total);
        $this->assign('list',$list);
        return $this->boye_display();
    }

    /*
     * 获取 Cp1000
     */
    private function getCp1000Info(){
        $cnt = $this->requestApi('By_SunsunCp1000_clientCount');
        $result = (new Cp1000Device())->whereExp('tcp_client_id','','neq')->count();
        return [
            'true_count'=>$result,
            'name'=>'Cp1000设备',
            'count'=>$cnt
        ];
    }


    /*
     * 获取 SServer
     */
    private function getSServerInfo(){
        $cnt = $this->requestApi('By_SunsunSServer_clientCount');
        return [
            'true_count'=>0,
            'name'=>'通用设备',
            'count'=>$cnt
        ];
    }

    /*
     * 获取 adt
     */
    private function getAdtInfo(){
        $cnt = $this->requestApi('By_SunsunAdt_clientCount');
        $result = (new AdtDevice())->whereExp('tcp_client_id','','neq')->count();
        return [
            'true_count'=>$result,
            'name'=>'Adt灯设备',
            'count'=>$cnt
        ];
    }

    /*
     * 获取 过滤桶
     */
    private function getFilterInfo(){
        $cnt = $this->requestApi('By_SunsunFilterVat_clientCount');         $result = (new FilterVatDevice())->whereExp('tcp_client_id','','neq')->count();
        return [
            'true_count'=>$result,
            'name'=>'过滤桶设备',
            'count'=>$cnt
        ];
    }
    /*
     * 获取 加热棒
     */
    private function getHeatingRodInfo(){
        $cnt = $this->requestApi('By_SunsunHeatingRod_clientCount');        $result = (new HeatingRodDevice())->whereExp('tcp_client_id','','neq')->count();
        return [
            'true_count'=>$result,
            'name'=>'加热棒设备',
            'count'=>$cnt
        ];
    }

    /*
     * 获取变频水泵
     */
    private function getWaterPumpInfo(){
        $groupCnt = $this->requestApi('By_SunsunWaterPump_clientCount');
        $result = (new WaterPumpDevice())->whereExp('tcp_client_id','','neq')->count();
        return [
            'true_count'=>$result,
            'name'=>'变频水泵设备',
            'count'=>(intval($groupCnt))
        ];
    }

    /*
     * 获取aph300
     */
    private function getAph300Info(){
        $cnt = $this->requestApi('By_SunsunAph300_clientCount');
        $result = (new Aph300Device())->whereExp('tcp_client_id','','neq')->count();
        return [
            'true_count'=>$result,
            'name'=>'aph300设备',
            'count'=>$cnt
        ];
    }

    /**
     * 获取aq806的设备信息
     * @return array
     */
    private function getAq806Info(){
        $cnt = $this->requestApi('By_SunsunAq806_clientCount');
        $result = (new Aq806Device())->whereExp('tcp_client_id','','neq')->count();
        return [
            'true_count'=>$result,
            'name'=>'aq806设备',
            'count'=>$cnt
        ];
    }

    /**
     * @param $type
     * @return mixed
     */
    public function requestApi($type){
        $data = [
            'type'=>$type,
            'api_ver'=>100,
            'alg'=>'md5_v3',
        ];

        $api = ByApiHelper::getInstance();

        $result = $api->callRemote($data);
        $info = $result['info'];
        if(is_array($info) && array_key_exists('all_client_cnt',$info)){
            return $info['all_client_cnt'];
        }
        return json_encode($result['info']);
    }
}